package com.fx.service.impl;

import cn.hutool.core.date.DateUtil;
import com.fx.entity.AdminTempUser;
import com.fx.entity.CstInfo;
import com.fx.entity.SysCustomSetting;
import com.fx.entity.SysUser;
import com.fx.mapper.SysCustomSettingMapper;
import com.fx.mapper.SysUserMapper;
import com.fx.service.CstInfoService;
import com.fx.service.SysUserService;
import com.fx.util.ConvertUtil;
import com.fx.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    IdWorker idWorker;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysCustomSettingMapper sysCustomSettingMapper;
    @Autowired
    CstInfoService cstInfoService;

    @Override
    public SysUser getById(String id) {
        return sysUserMapper.getById(id);
    }

    @Override
    public List<SysUser> listData(Map<String, Object> param) {
        return sysUserMapper.queryListData(param);
    }

    @Override
    public int getCount(Map<String, Object> param) {
        return sysUserMapper.queryCount(param);
    }

    @Override
    public int save(SysUser sysUser) {
        sysUser.setFlId(String.valueOf(idWorker.nextId()));
        sysUser.setFlCreatedTime(DateUtil.now());
        return sysUserMapper.save(sysUser);
    }

    @Override
    public int updateById(SysUser sysUser) {
        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public int batchDelByIds(String[] ids) {
        return sysUserMapper.batchRemove(ids);
    }

    /**
     * 方法名称：系统用户登录
     * 概要说明：根据用户名和密码登录
     *
     * @param userName
     * @Ather EDZ
     * @Date 2018-5-18 11:37
     * @Param sysUser 登录用户对象
     * @Return
     */
    @Override
    public AdminTempUser getByLoginName(String userName) {
        AdminTempUser tempUser = new AdminTempUser();
        SysUser user = sysUserMapper.queryByUserName(userName);
        if (user != null) {
            tempUser.setFlId(user.getFlId());
            tempUser.setFlName(user.getFlName());
            tempUser.setFlPwd(user.getFlLoginPwd());
            tempUser.setFlRoleId(user.getFlRoleId());
            tempUser.setFlStatus(user.getFlStatus());
            tempUser.setFlType(1);
            tempUser.setFlAvatar(user.getFlAvatar());
            tempUser.setFlSex(user.getFlSex());
            tempUser.setFlEmail(user.getFlEmail());
            return tempUser;
        } else {
            CstInfo cstInfo = cstInfoService.getByLoginName(userName);
            if (cstInfo != null) {
                tempUser.setFlId(cstInfo.getFlId());
                tempUser.setFlName(cstInfo.getFlName());
                tempUser.setFlPwd(cstInfo.getFlLoginPwd());
                tempUser.setFlRoleId(cstInfo.getFlRoleId());
                tempUser.setFlType(0);
                tempUser.setFlAvatar(cstInfo.getFlAvatar());
                tempUser.setFlSex(cstInfo.getFlSex());
                tempUser.setFlEmail(cstInfo.getFlEmail());
                if (cstInfo.getFlLaveDay() == -1 || cstInfo.getFlLaveDay() <= 0)
                    tempUser.setFlStatus(1);
                return tempUser;
            }
            return null;
        }
    }

    @Override
    public void updateCurrentUserSetting(Map<String, Object> param) throws Exception {
        String userId = String.valueOf(param.get("userId"));
        param.remove("userId");
        SysCustomSetting setting = sysCustomSettingMapper.queryByUserId(userId);
        if (setting == null || setting.getFlId() == null) {
            //创建
            setting = new SysCustomSetting();
            setting.setFlId(String.valueOf(idWorker.nextId()));
            setting.setFlUserId(userId);
            setting.setFlBoxLay("0");
            setting.setFlBoxBackImg("/common/premium/boxed-bg/blurred/bg/2.jpg");
            setting.setFlAnimate("1");
            setting.setFlAnimateType("easeOutBack");
            setting.setFlFixedNavbar("0");
            setting.setFlFixedFooter("1");
            setting.setFlShowUserImg("1");
            setting.setFlShowShortCut("1");
            setting.setFlNavColl("1");
            setting.setFlOffType("none");
            setting.setFlShowAsd("0");
            setting.setFlFixedAsd("0");
            setting.setFlFloatAsd("0");
            setting.setFlLeftAsd("0");
            setting.setFlDarkAsd("1");
            setting.setFlColorTheme("/common/css/themes/type-c/theme-mint.min.css");
            sysCustomSettingMapper.save(setting);
        }
        Object[] tempArr = param.keySet().toArray();
        Object value = null;
        Object key = null;
        for (Object o : tempArr) {
            value = param.get(String.valueOf(o));
            key = o;
        }

        Class clazz = setting.getClass();
        Method method = clazz.getDeclaredMethod("set" + ConvertUtil.toUpperFirstChar(ConvertUtil.camelCaseName(String.valueOf(key))), String.class);
        method.invoke(setting, value);
        sysCustomSettingMapper.update(setting);
    }

    @Override
    public SysCustomSetting getCurrentSettingByUserId(String userId) {
        return sysCustomSettingMapper.queryByUserId(userId);
    }
}
